package com.aidysoft.practice.easypoi.demo2.controller;

import cn.afterturn.easypoi.entity.ImageEntity;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.aidysoft.practice.easypoi.utils.ExcelUtil;
import com.aidysoft.practice.easypoi.utils.style.ExcelStyleUtil;
import com.aidysoft.practice.easypoi.demo2.entity.EasyPOIModel;
import com.aidysoft.practice.easypoi.demo2.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Controller
public class ExportMoreSheetWithTemplate {

    /**
     * 使用easypoi导出文件
     * 单sheet / 多sheet
     *
     * @param response
     * @return
     */
    @RequestMapping(value = "exportexceltest")
    @ResponseBody
    public String exportExcelTest(HttpServletResponse response) {

        // 查询数据,此处省略
        List<EasyPOIModel> list = new ArrayList<>();
        int count1 = 0;
        // 学生信息
        User user1 = new User("张三1", "男", 20, "北京市东城区", "篮球");
        User user2 = new User("李四1", "男", 17, "北京市西城区", "游泳");
        User user3 = new User("淑芬1", "女", 34, "北京市丰台区", "唱歌，跳舞");
        User user4 = new User("仲达1", "男", 55, "北京市昌平区", "象棋，足球");
        // sheet1内容
        EasyPOIModel easyPOIModel11 = new EasyPOIModel(String.valueOf(count1++), "信科", user1);
        EasyPOIModel easyPOIModel12 = new EasyPOIModel(String.valueOf(count1++), "生工", user2);
        EasyPOIModel easyPOIModel13 = new EasyPOIModel(String.valueOf(count1++), "化工", user3);
        EasyPOIModel easyPOIModel14 = new EasyPOIModel(String.valueOf(count1++), "信科", user4);
        list.add(easyPOIModel11);
        list.add(easyPOIModel12);
        list.add(easyPOIModel13);
        list.add(easyPOIModel14);
        // 存放数据map
        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("list", list);

        // sheet2内容
        List<User> userList = new ArrayList<User>();
        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        userList.add(user4);
        map1.put("lists", userList);

        //sheet3
        List<Map<String, Object>> pictureList = new ArrayList<Map<String, Object>>();
        // 创建数据对象
        Map<String, Object> temp = new HashMap<String, Object>();
        // 创建图片
        ImageEntity image = new ImageEntity();
        image.setHeight(1000);
        image.setWidth(2500);
        image.setUrl("templates/imgs/1.jpg");
        //
        temp.put("name", "福字");
        temp.put("image", image);
        //
        pictureList.add(temp);
        // 创建数据对象
        Map<String, Object> temp1 = new HashMap<String, Object>();
        // 创建图片
        ImageEntity image1 = new ImageEntity();
        image1.setHeight(2000);
        image1.setWidth(5000);
        image1.setUrl("templates/imgs/2.jpg");
        //
        temp1.put("name", "树");
        temp1.put("image", image1);
        //
        pictureList.add(temp1);
        //
        map1.put("i", pictureList);


        // 设置导出配置
        // 获取导出excel指定模版
        TemplateExportParams params = new TemplateExportParams("templates/easypoiExampleMore.xlsx", true);
        // 设置sheetName，若不设置该参数，则使用得原本得sheet名称
        String[] sheetNameArray = {"班级信息", "学生信息", "图片"};
        params.setSheetName(sheetNameArray);
        params.setStyle(ExcelStyleUtil.class);
        ExcelUtil.exportTemplateExcel(map1, params, "测试excel.xlsx", response);
        return "success";
    }

}